   <?php
        session_start();

        require '../classes/imageClass.php';
        require '../classes/albumClass.php';
        require'../classes/connectionFactoryClass.php';
        require'../classes/imageDao.php';
        require'../classes/albumDao.php';
        require'../classes/validates.php';
        require'checksDate.php';
        require'../classes/createFolderClass.php';

        $image = new ImageClass();
        $album = new AlbumClass();
        $albumCadastro = new AlbumClass();
        $date = new checksDate();
        $c = new ConnectionFactory();
        $db = $c->getConnection();
        $imageDao = new imageDao($db);
        $albumDao = new albumDao($db);
        $valid = new validates();
        $folder = new createFolderClass();



        $directory = 'uploads/';
        $directoryTmp = '../temp/';
        $user = $_SESSION['user'];
        $userId = $_SESSION['userId'];

        $filePhoto = array();
        $filePhotoType = array();
        $filePhotoLegend = array();


        $filePhotoName = $_POST['fotoname'];
        $filePhotoType = $_POST['fototype'];
        $filePhotoLegend = $_POST['legend'];
        $albumName = strtoupper($_POST['album']);



        $continue = false;
        $result = false;
        $albumExiste = false;
        $photoCapa = null;
        $albumCriado = null;
        $folderCreated = false;

        if (isset($_POST['existe']))
            {
                $continue = true;
                $albumExiste = true; //Não veio foto capa, então album existe
            } else if (!isset($_POST['capa']) && !isset($_POST['existe']))
                {
                    $continue = false;
                    echo "<script> alert('Selecione uma foto para capa.'); 
                    window.location='../home/upload.php';</script>";
                }else
                    $continue = true; //Album não existe e foto capa selecionada continue  ';

        
                
        if ($continue) 
            {
                        if ($albumExiste == false) //CADASTRA NOVO ALBUM
                            { 
                                  $photoCapa = $_POST['capa'];
                                   $album->setIdUser($userId);
                                   $album->setNameAlbum($albumName);
                                   $album->setPhotoCapa($photoCapa);
                                   $album->setDirectory($directory);
                                   $album->setDateInclusionAlbum($date->dataAtual());

                                   $albumCriado = $albumDao->create($album);
                             }

                   if ($albumCriado || $albumExiste) 
                       {
                        $albumCadastro = $albumDao->getAlbumByNameEqual($albumName, $userId);
                        $idAlbum = $albumCadastro->getIdAlbum();
                        $image->setAlbumId($idAlbum);
                       }else
                       $result = false;

                  
                       if ($albumExiste == false) 
                           {
                                $folderCreated = $folder->createFolder($directory, $user, $albumName);
                           }else
                                $folderCreated = true;


                   if ($folderCreated)
                       {
                            for ($i = 0; $i < count($filePhotoName); $i++)
                            {
                                $image->setName_photo($filePhotoName[$i]);
                                $image->setType($filePhotoType[$i]);
                                $image->setLegend($filePhotoLegend[$i]);
                                $tmpFilePath = $directoryTmp . $filePhotoName[$i];
                                $image->setDateInclusionPhoto($date->dataAtual());

                                //verifica se foto existe;
                                if ($tmpFilePath != "") 
                                    {
                                     $dir = $newFilePath = '../' . $directory . '/' . $user . '/' . $albumName . '/' . $image->getName_photo();
                                      //Copia a foto para o album

                                         if (copy($tmpFilePath, $newFilePath)) 
                                             {
                                                 unlink($tmpFilePath);

                                                 //grava os dados da foto no banco
                                                 $result = $imageDao->create($image);
                                                 //include '../crop/marcaDagua.php';
                                              }else
                                                     echo"<script> alert('O arquivo .$image->getName_photo(). Não foi possível fazer upload'); 
                                                     window.location='../upload.php';</script>";
                                     } else {
                                             echo "<script> alert('Selecione um arquivo'); 
                                             window.location='../home/upload.php';</script>";
                                             }
                            }//Fim do for
                        } else {
                                //Se não criou a pasta, deleta album cadastrado no banco
                                $albumDao->delete($albumCadastro->getIdAlbum());
                                $result = false;
                               }
        }
        ?>





        <script>

            //<meta http-equiv="content-type" content="text/html;charset=utf-8" />;
            var result = <?php echo $result; ?>;


            if (result) {

                alert("Upload realizado com sucesso!");
                setTimeout("document.location = '../index.php'", 500);

            } else {

                alert("Não Foi Possível Realizar o upload!");
                setTimeout("document.location = '../index.php'", 500);
            }


        </script>

